<?php

/**
 * Description of SitemapController
 * 
 * @author <KTD>
 * @since May 7, 2013 - 2:29:25 PM
 */
class SitemapController extends MyController
{

    public function actionIndex()
    {
        $this->assetDir = Yii::app()->createAbsoluteUrl('/') . "/themes/" . Yii::app()->theme->name . '/assets';
        $this->layout   = "blank";
        if (!$sitemap        = Yii::app()->cache->get('sitemap'))
        {
            $items = array();

            $category = Category::model()->findAll(array('condition' => "status!=" . Category::STATUS_DRAFT));
            foreach ($category as $item)
            {
                $entry['lastmod'] = NULL;
                if ($item->updated != null)
                {
                    $entry['lastmod'] = $item->updated->created_at;
                }
                else
                {
                    $entry['lastmod'] = $item->created_at;
                }
                $entry['link']     = $item->getPublicUrl(TRUE);
                $entry['freq']     = 'daily';
                $entry['priority'] = 1;
                $items[]           = $entry;
            }

            $post = Post::model()->findAll(array('condition' => "status!=" . Post::STATUS_DRAFT));
            foreach ($post as $item)
            {
                $entry['lastmod'] = NULL;
                if ($item->updated != null)
                {
                    $entry['lastmod'] = $item->updated->created_at;
                }
                $entry['link']     = $item->getPublicUrl(TRUE);
                $entry['freq']     = 'daily';
                $entry['priority'] = 0.9;
                $items[]           = $entry;
            }
            $products = Product::model()->findAll(array('condition' => "status!=" . Product::STATUS_DRAFT));
            foreach ($products as $item)
            {
                $entry['lastmod'] = NULL;
                if ($item->updated != null)
                {
                    $entry['lastmod'] = $item->updated->created_at;
                }
                $entry['link']     = $item->getPublicUrl(TRUE);
                $entry['freq']     = 'daily';
                $entry['priority'] = 0.1;
                $items[]           = $entry;
            }
            $products1 = Product::model()->findAll(array('condition' => "status!=" . Product::STATUS_DRAFT));
            foreach ($products as $item)
            {
                $entry['lastmod'] = NULL;
                if ($item->updated != null)
                {
                    $entry['lastmod'] = $item->updated->created_at;
                }
                $entry['link']     = $item->getRegisterUrl(TRUE);
                $entry['freq']     = 'daily';
                $entry['priority'] = 0.7;
                $items[]           = $entry;
            }

            array_multisort($items, SORT_DESC);

            $mode    = current($items);
            $lastmod = $mode['lastmod'];

            $home_vi           = Yii::app()->createAbsoluteUrl('site/index');
            $entry['lastmod']  = $lastmod;
            $entry['link']     = $home_vi;
            $entry['freq']     = 'daily';
            $entry['priority'] = 1;
            $unshift[]         = $entry;
            array_unshift($items, $unshift[0]);
            $data['items']     = $items;

            $sitemap = $this->render('index', $data, true);
            Yii::app()->cache->set('sitemap', $sitemap, 300);
        }
        header("Content-Type: text/xml"); // important!   
        echo $sitemap;
    }

}
